#include <stdio.h>

/*
Including library to remove the following compiler warnings:
 warning: incompatible implicit declaration of built-in function ‘malloc’
 warning: incompatible implicit declaration of built-in function ‘exit’
*/
#include <stdlib.h>

int main(int argc, char* argv[])
{
	// declare two uninitialised integer variables
	int i, n;

	// declare a character pointer
	char * buffer;
	
	// Print a message to the screen to prompt a user for a string length
	printf("How long do you want the string? ");

	//Read stdin and assign the input to the integer variable i
	scanf("%d", &i);
	
	// Allocate memory to the size of the user input value plus string termination character
	buffer = (char*) malloc (i+1);

	// If memory fails to be allocated
	if (buffer == NULL)
	{
		// Exit the code in a controlled manner
		exit (1);
	}

	//Using a for loop up to the size of the user input
	for (n=0; n<i; n++) 
	{
		// add a random integer value to the array index of the string for that character
		buffer[n] = rand() % 26 +'a';
	}

	// Add the string termination character as the last element in the array
	buffer[i]='\0';

	// Print the random string to the stdout
	printf ("Random string: %s\n",buffer);

	// Free the memory allocated to the string
	free (buffer);
}
